<template>
  <div class="header">
    <!-- 头部组件 -->
    <el-row style="height:100%;">
      <el-col :span="$i18n.locale=='zh_cn'? 2 : 3">
        <h1 class="title">{{$t("SYS.programName")}}</h1>
      </el-col>
      <el-col :span="1" style="height:100%;text-align:center;">
        <!-- <div  @click="changeCollspace" style="position:relative;height:100%;">
          <transition name="menuFade">
            <i class="el-icon-menu" style="width:100%;text-align:center;line-height:60px;font-size:30px" key="open" v-if="collspace == false"></i>
            <i class="el-icon-s-grid" style="width:100%;text-align:center;line-height:60px;font-size:30px" key="close" v-else></i>
          </transition>
        </div> -->
      </el-col>
      <el-col :span="$i18n.locale=='zh_cn'? 18 : 17" style="min-height:1px;"></el-col>
      <el-col :span="1" style="height:100%;">
        <div class="changeLanguage" @click="showLanguage">
          <el-dropdown trigger="click" @command="handleCommandLanguage" class="el_dropdown_language">
            <span class="el-dropdown-link">
              {{languageName[$i18n.locale]}}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="i" v-for="(item,i) in languageName" :key="i">{{item}}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-col>
      <el-col :span="1" style="height:100%;">
        <div class="user-info" @click="showMenu">
          <el-dropdown trigger="click" @command="handleCommand" class="el_dropdown">
            <span class="el-dropdown-link">
              {{$store.state.userInfo.realName}}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="editPassWord">{{$t('SYS.editPassword')}}</el-dropdown-item>
              <el-dropdown-item command="loginOut">{{$t("SYS.logOut")}}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
    <updatePassWord v-if="isShow" ref="updatePassWord" @closedDialog="closedDialog"></updatePassWord>
  </div>
</template>

<script>
import { clearLoginInfo } from "@/utils";
import updatePassWord from "./update-passWord.vue";
export default {
  name:"top-header",
  components:{updatePassWord},
  data(){
    return{
      isShow:false,
      collspace:false,
      languageName:{
        zh_cn:"中文",
        en_us:"English"
      }
    }
  },
  methods:{
    changeCollspace(){
      if(this.collspace){
        this.collspace = false
      }else{
        this.collspace = true
      }
      this.$store.commit('setCollspace',this.collspace)
    },
    handleCommandLanguage(val){
      this.$store.commit('setSysLanguage',val)
    },
    showMenu(){
      document.querySelector('.el_dropdown .el-dropdown-link.el-dropdown-selfdefine').click()
    },
    showLanguage(){
      document.querySelector('.el_dropdown_language .el-dropdown-link.el-dropdown-selfdefine').click()
    },
    loginOut(){
      this.$confirm(this.$t('SYS.isLogOut'), this.$t("SYS.tip"), {
          confirmButtonText: this.$t('SYS.check'),
          cancelButtonText: this.$t("SYS.cancel"),
          type: 'warning'
        }).then(()=>{
          this.out()
        }).catch(()=>{})
    },
    out(){
      clearLoginInfo();
      this.$router.replace({ path:'/login' })
      return
      this.$http.post(this.$baseUrl.logout).then((res)=>{
        if (res.data.code !== 0) {
          return this.$message.error(res.msg);
        }
        clearLoginInfo();
        this.$router.replace({ path:'/login' })
      }).catch((err)=>{console.error(err)})
    },
    updatePassWord(){
      this.isShow = true
      this.$nextTick(()=>{
        this.$refs.updatePassWord.init()
      })
    },
    closedDialog(){
      this.isShow = false
    },
    handleCommand(val){
      if(val == 'loginOut'){
        this.loginOut()
      }else{
        this.updatePassWord()
      }
    }
  }
}
</script>

<style scoped>
.header{
  height: inherit;
}
.header .user-info{
  width: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  height: inherit;
}
.title{
  line-height: 60px;
  font-size: 24px;
  text-align: center;
}
.changeLanguage{
  display: flex;
  height: inherit;
  align-items: center;
  cursor: pointer;
}
.menuFade-enter-active, .menuFade-leave-active {
  transition: 0.5s;
  position: absolute;
  left: 0;
  /* transform: translateX(-50%); */
  top: 0;
}
.menuFade-enter{
  opacity: 0;
  transform:  rotate(-180deg);
}
.menuFade-leave-to{
  opacity: 0;
  transform: rotate(180deg);
}
/* .menuFade-enter, .menuFade-leave-to  {
  opacity: 0;
  transform: rotate(180deg);
} */
</style>